/*!
    \file  main.c
    \brief running led
    
    \version 2019-6-5, V1.0.0, firmware for GD32VF103
*/

/*
    Copyright (c) 2019, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32vf103.h"
#include "systick.h"
#include <stdio.h>
#include "gd32vf103_eclic.h"


void delay(int cnt){
	while(cnt--){
		int i=0;
		for(i=0;i<1000;i++)
			asm("nop");
	}
}



// ************************************************************************************************************** //
//USART0初始化
void USART0_Init()
{
	rcu_periph_clock_enable(RCU_GPIOA);  //使能GPIOA时钟
	rcu_periph_clock_enable(RCU_USART0); //使能USART0时钟
	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);        //USART_TX
	gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10); //USART_RX
	usart_deinit(USART0);
	usart_baudrate_set(USART0, 115200U);          //波特率 115200
	usart_word_length_set(USART0, USART_WL_8BIT); //8位数据位
	usart_stop_bit_set(USART0, USART_STB_1BIT);   //1位停止位
	usart_parity_config(USART0, USART_PM_NONE);   //无奇偶校验
	usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
	usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
	usart_receive_config(USART0, USART_RECEIVE_ENABLE); //使能USART接收
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); //使能USART发送
	usart_interrupt_enable(USART0,USART_INT_RBNE); //使能接收中断
	eclic_irq_enable(USART0_IRQn,1,0); //选择USART0中断线,中断优先级和子优先级
	usart_enable(USART0); //使能USART0
}
// ************************************************************************************************************** //
//USART0中断服务函数
void USART0_IRQHandler(void)
{
	uint8_t Usart_rec = 0;
    if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) //接收中断
    {
    	Usart_rec = usart_data_receive(USART0);
        usart_data_transmit(USART0, Usart_rec);
    }
    //if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) //发送中断
    //{
    //}
}

extern void timer_begin();
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    int i;
    eclic_global_interrupt_enable();
	eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
    timer_begin();
    USART0_Init();
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1|GPIO_PIN_2);
	printf("start%d\n",1);
    while(1){
        ++i;		
    }
}

/* retarget the C library printf function to the USART */
int _put_char(int ch)
{
    usart_data_transmit(USART0, (uint8_t) ch );
    while ( usart_flag_get(USART0, USART_FLAG_TBE)== RESET){
    }

    return ch;
}